﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class CATAssignment: ICATAssignment
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public CATAssignment(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.CATAssignment GetCATAssignment(System.Int32 cATAssignmentID)
		{
			BEGDC.PreContract.DAL.CATAssignment cATAssignment = BEGDC.PreContract.DAL.CATAssignment.TryFind(cATAssignmentID);

            return cATAssignment;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetCATAssignment_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM CATAssignment";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteCATAssignment(System.Int32 cATAssignmentID)
		{
			try
            {
				BEGDC.PreContract.DAL.CATAssignment cATAssignment = BEGDC.PreContract.DAL.CATAssignment.TryFind(cATAssignmentID);
                cATAssignment.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CATAssignment", string.Format("Error occured when deleting the CATAssignment, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddCATAssignment(BEGDC.PreContract.DAL.CAT cATID, BEGDC.PreContract.DAL.Employee employeeID)	
		{
			try
            {
				BEGDC.PreContract.DAL.CATAssignment cATAssignment = new BEGDC.PreContract.DAL.CATAssignment();
				
				cATAssignment.CATID = cATID;
				cATAssignment.EmployeeID = employeeID;
				cATAssignment.CreateAndFlush();
				
				return cATAssignment.CATAssignmentID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CATAssignment", string.Format("Error occured when adding a new CATAssignment, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateCATAssignment(System.Int32 cATAssignmentID, BEGDC.PreContract.DAL.CAT cATID, BEGDC.PreContract.DAL.Employee employeeID)
		{
			try
            {
                BEGDC.PreContract.DAL.CATAssignment cATAssignment = GetCATAssignment(cATAssignmentID);

				cATAssignment.CATID = cATID;
				cATAssignment.EmployeeID = employeeID;
				cATAssignment.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("CATAssignment", string.Format("Error occured when updating the CATAssignment, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



